package System_code;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-08-10
 * Time: 23:38
 */
public class Demo29 {
    public static void main(String[] args) throws InterruptedException {
        // 构造方法传入有效资源的个数
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                try {
                    // P 操作 申请资源
                    System.out.println("申请资源");
                    semaphore.acquire();
                    System.out.println("我申请到资源了");
                    // V 操作 释放资源
                    System.out.println("我释放资源了");
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for(int i=0;i<20;i++){
            Thread t=new Thread(runnable);
            t.start();
        }
    }
}
